package com.dst.smart.json;

import java.io.File;
import org.json.JSONObject;

import com.dst.smart.app.MyApplication;
import com.dst.smart.R;
import com.dst.smart.constant.Constants;
import com.dst.smart.model.Master;
import com.dst.smart.tools.SystemInfo;

import android.content.Context;
import android.text.TextUtils;

public class MasterUtils {
	public static Master masterInfo = null;
	private static File jsonFile = new File(Constants.REMOTE_PATH
			+ File.separator + "master_json");

	public static Master getMaster(Context context) {
		if (masterInfo == null) {
			String json = JSONFileUtils.getJsonStringFromFile(jsonFile)
					.replace("\r\n", "");
			if (TextUtils.isEmpty(json)) {
				masterInfo = new Master();

				masterInfo.setMn(MyApplication.getInstance().getString(
						R.string.app_name));
				masterInfo.setMc(SystemInfo.getMasterID(MyApplication
						.getInstance()));
				masterInfo.setWf(SystemInfo.getSSID(context));
				masterInfo.setSt(1);
				masterInfo.setUn("admin");
				masterInfo.setTy(0);
				masterInfo.setPw("");
				masterInfo.setOr("user");
				masterInfo.setOp("1234");
				saveMasterInfo();
			} else {
				try {
					JSONObject object = new JSONObject(json);
					masterInfo = new Master();
					masterInfo.setMn(object.getString("MasterName"));
					masterInfo.setMc(object.getString("MasterMac"));
					masterInfo.setWf(SystemInfo.getSSID(context));
					masterInfo.setSt(1);
					masterInfo.setUn(object.getString("UserName"));
					masterInfo.setPw(object.getString("PassWord"));
					masterInfo.setTy(0);
					masterInfo.setOp(object.getString("OperatorPwd"));
					masterInfo.setOr(object.getString("MasterOperator"));

				} catch (Exception e) {
					// TODO: handle exception
					masterInfo = new Master();
					masterInfo.setMn(MyApplication.getInstance().getString(
							R.string.app_name));
					masterInfo.setMc(SystemInfo.getMasterID(MyApplication
							.getInstance()));
					masterInfo.setWf(SystemInfo.getSSID(context));
					masterInfo.setSt(1);
					masterInfo.setUn("admin");
					masterInfo.setTy(0);
					masterInfo.setPw("");
					masterInfo.setOr("user");
					masterInfo.setOp("1234");
					saveMasterInfo();
				}
			}
		}
		return masterInfo;

	}

	public static void saveMasterInfo() {
		try {
			if (masterInfo != null) {
				JSONObject bject = new JSONObject();
				bject.put("MasterMac", masterInfo.getMc());
				bject.put("UserName", masterInfo.getUn());
				bject.put("MasterName", masterInfo.getMn());
				bject.put("PassWord", masterInfo.getPw());
				bject.put("MasterType", masterInfo.getTy());
				bject.put("MasterOperator", masterInfo.getOr());
				bject.put("OperatorPwd", masterInfo.getOp());
				JSONFileUtils.saveJSONToFile(jsonFile, bject.toString());
			}
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}

	public static void reset() {
		if (masterInfo == null) {
			masterInfo = new Master();
		}
		masterInfo.setMn(MyApplication.getInstance().getString(
				R.string.app_name));
		masterInfo.setMc(SystemInfo.getMasterID(MyApplication.getInstance()));
		masterInfo.setWf(SystemInfo.getSSID(MyApplication.getInstance()));
		masterInfo.setSt(1);
		masterInfo.setUn("admin");
		masterInfo.setTy(0);
		masterInfo.setPw("");
		masterInfo.setOr("");
		masterInfo.setOp("");
		saveMasterInfo();
	}
}
